package com.autumn.mybatis.factory;

import com.autumn.mybatis.provider.IDbProvider;
import com.autumn.spring.boot.context.SrpingServiceContextHolder;
import com.autumn.util.ExceptionUtils;
import com.autumn.util.StringUtils;

/**
 * 动态服务请求线程上下文数据源
 * 
 * @author 老码农
 *
 *         2017-12-29 17:36:10
 */
public class DynamicRequestServiceContextRoutingDataSource extends AbstractDynamicRoutingKeyDataSource {

	/**
	 * 实例化
	 * 
	 * @param dataSourceFactory
	 * @param provider
	 */
	public DynamicRequestServiceContextRoutingDataSource(IDataSourceFactory dataSourceFactory, IDbProvider provider) {
		super(dataSourceFactory, provider);
	}

	@Override
	public String getRoutingKey() {
		String routingKey = SrpingServiceContextHolder.getRequestServiceId();
		if (StringUtils.isNullOrBlank(routingKey)) {
			ExceptionUtils.throwSystemException("当前请求 serviceId 为 null 或空白值。");
		}
		return routingKey;
	}
}
